package com.kl.common.util;


import org.apache.commons.beanutils.BeanPredicate;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.Predicate;
import org.apache.commons.collections.PredicateUtils;
import org.apache.commons.collections.functors.EqualPredicate;

import java.util.Collection;
import java.util.List;

/**
 * 泛型集合数据基础工具类
 *
 * @author xr_zjb
 * @date 2017/09/18 14:14
 *
 */
public class ListDataHandleUtil {

    /**
     * List 数据中筛选
     * @param field 筛选字段
     * @param values 值
     * @param list 原list
     * @param type 类
     * @param <T>
     * @return
     */
    public static <T> List<T> listHandleDataByField(String field, Object values, List list, Class<T> type){
        EqualPredicate parameter = new EqualPredicate(values);
        BeanPredicate tableCoulmn_paramerter = new BeanPredicate(field, parameter);
        Predicate[] allPredicateArray = {tableCoulmn_paramerter };
        Predicate allPredicate = PredicateUtils.allPredicate(allPredicateArray);
        Collection<T> filteredCollection = CollectionUtils.select(list, allPredicate);
        return (List)filteredCollection;
    }

}
